package com.hjx.windowtool.bk.dibaqu.server;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationContext;

import com.hjx.windowtool.util.CarparkDataBaseUtil;
import com.zaxxer.hikari.HikariDataSource;



@SpringBootApplication
public class BookApplication {
	private static final String SQLSERVER = "SQLSERVER";
	public static void main(String[] args) {
		start(args);
	}

	/**
	 * @param args
	 */
	public static void start(String[] args) {
		Properties p = new Properties();
		p.setProperty("server.port", "9081");
		p.setProperty("spring.jpa.show-sql", "false");
		p.setProperty("spring.jpa.hibernate.ddl-auto", "update");
		getDatebaseConfig(p,SQLSERVER);
		p.setProperty("logging.config", "${user.dir}/logback.xml");
		SpringApplicationBuilder builder = new SpringApplicationBuilder(BookApplication.class);
		ApplicationContext context=builder.headless(false).web(WebApplicationType.NONE).properties(p).run(args);
		ContextUtil.setContext(context);
	}

	/**
	 * @param p
	 */
	public static void getDatebaseConfig(Properties p,String type) {
		switch (type) {
		case SQLSERVER:
			String createTableSql=CarparkDataBaseUtil.getDefaultCreateDatabaseSql(CarparkDataBaseUtil.SQLSERVER2008, System.getProperty("user.dir")+"\\", "hjx");
//			if(Files.exists(Paths.get(System.getProperty("user.dir")+"\\"+"hjx.mdf") )) {
//				createTableSql="IF NOT EXISTS(SELECT * FROM sysDatabases WHERE name='hjx') CREATE DATABASE hjx ON (FILENAME = '"+System.getProperty("user.dir")+"\\hjx.mdf'),(FILENAME = '"+System.getProperty("user.dir")+"\\hjx.ldf') FOR ATTACH";
//			}
//			System.out.println(createTableSql);
			String dialect = "org.hibernate.dialect.SQLServer2005Dialect";
			String driver = "net.sourceforge.jtds.jdbc.Driver";
			String url = "jdbc:jtds:sqlserver://localhost:1433/hjx";
			String username = "sa";
			String password = "1";
			CarparkDataBaseUtil.executeSQL("127.0.0.1", "1433", "master", username, password, createTableSql, CarparkDataBaseUtil.SQLSERVER2008);
			p.setProperty("spring.jpa.database-platform", dialect);
			p.setProperty("spring.datasource.driver-class-name", driver);
			p.setProperty("spring.datasource.url", url);
			p.setProperty("spring.datasource.username", username);
			p.setProperty("spring.datasource.password", password);
			p.setProperty("spring.datasource.hikari.connection-test-query", "select 1");
			p.setProperty("spring.datasource.hikari.minimum-idle", "1");
			break;
		default:
			p.setProperty("spring.jpa.database-platform", "org.hibernate.dialect.H2Dialect");
			p.setProperty("spring.datasource.driver-class-name", "org.h2.Driver");
			p.setProperty("spring.datasource.url", "jdbc:h2:file:./hjxH2DB;FILE_LOCK=SOCKET");
			p.setProperty("spring.datasource.username", "sa");
			p.setProperty("spring.datasource.password", "123456");
			break;
		}
	}
	public static void stop() {
		SpringApplication.exit(ContextUtil.getContext());
	}
}
